package ngat.oss.simulation.metrics;

import ngat.oss.simulation.*;
import ngat.astrometry.*;
import ngat.phase2.*;

/** Compute the ENV fractions under U/P/A/X categories per night from a predictor.*/
public class EnvironmentFractionProfileCalculator {

    
    /** Calculate a profile of fractions of night by category between start and end (nights).*/
    public static EnvironmentFractionProfile getFractionProfile(Site site, EnvironmentPredictor ep, long start, long end) {
	
	int nn = (int)((double)(end - start)/86400000.0) + 1;
	long dt = 60000L; // timestep resolution

	// work out start of first obs night (should be 1200 local) we assume its just set anyway.
	double[] useless   = new double[nn];
	double[] poor      = new double[nn];
	double[] average   = new double[nn];
	double[] excellent = new double[nn];

	for (int in = 0; in < nn; in++) {
	    
	    long sn = start + in* 86400000L;
	    long en = sn + 86400000L;

	    excellent[in] = 0.0;
	    average[in]   = 0.0;  
	    poor[in]      = 0.0;
	    useless[in]   = 0.0;

	    long t = sn;
	    while (t < en) {
		
		Position sun = Astrometry.getSolarPosition(t);
		double sunel = sun.getAltitude(t, site);
		
		if (sunel < 0.0) {
		    EnvironmentSnapshot env = ep.predictEnvironment(t);
		    int see = env.seeing;
		    switch (see) {
		    case Group.EXCELLENT:
			excellent[in] += dt;
			break;
		    case Group.AVERAGE:
			average[in] += dt;
			break;
		    case Group.POOR:
			poor[in] += dt;
			break;
		    case Group.CRAP:
			useless[in] += dt;
			break;
		    }
		}	    
	    
		t += dt;
	    }

	}

	EnvironmentFractionProfile profile = new EnvironmentFractionProfile();
	profile.start = start;
	profile.end   = end;
	profile.unusable  = useless;
	profile.poor      = poor;
	profile.average   = average;
	profile.excellent = excellent;

	return profile;

    }


}
